/*
 * Copyright (c) 2006-2018, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2018-11-06     SummerGift   first version
 */

#define LOG_LVL DBG_LOG
#define LOG_TAG "main"
#include <ulog.h>

#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>
#include <dev_spi_flash_sfud.h>
#define APP_VERSION "1.0.0"
/* defined the LED0 pin: PB1 */
#define LED0_PIN GET_PIN(A, 15)
#define SPI20_CS GET_PIN(B, 6)

static struct rt_spi_device spi_dev_20;

extern char spi_store_1[];
extern char spi_store_2[];
extern char spi_store_3[];
extern char spi_store_4[];
int main(void)
{
    LOG_I("addr.spi_store_1=0x%x,spi_store_2=0x%x", spi_store_1,spi_store_2);
    LOG_I("addr.spi_store_3=0x%x,spi_store_4=0x%x", spi_store_3,spi_store_4);


    // 在总线上挂载设备
    if (RT_EOK != rt_spi_bus_attach_device_cspin(&spi_dev_20, "spi20", "spi2", SPI20_CS, RT_NULL))
    {
        rt_kprintf("Failed to attach the spi20 device in spi1 bus.");
        return -RT_ERROR;
    }
    // struct rt_spi_configuration cfg;
    // cfg.data_width = 8;
    // cfg.mode = RT_SPI_MASTER | RT_SPI_MODE_0 | RT_SPI_MSB;
    // cfg.max_hz = 20 * 1000 *1000;                           /* 20M */

    // rt_spi_configure(&spi_dev_20, &cfg);

        // SFUD探测flash
    rt_spi_flash_device_t spiflash_0 = RT_NULL;
    spiflash_0 = rt_sfud_flash_probe("spiflash0", spi_dev_20.parent.parent.name);
    if (RT_NULL == spiflash_0)
    {
        LOG_E("Failed to probe the spiflash0.");
        return -RT_ERROR;
    }
    else
    {
        // 输出flash的基本信息
        sfud_flash *sfud_dev = spiflash_0->user_data;
        LOG_I("spi Flash black device name:%s", sfud_dev->name);
        LOG_I("%s-->chip name:%s", sfud_dev->name, sfud_dev->chip.name);
        LOG_I("%s-->chip mf_id:0x%x", sfud_dev->name, sfud_dev->chip.mf_id);
        LOG_I("%s-->chip type_id:0x%x", sfud_dev->name, sfud_dev->chip.type_id);
        LOG_I("%s-->chip capacity_id:0x%x", sfud_dev->name, sfud_dev->chip.capacity_id);
        LOG_I("%s-->chip capacity:%d(byte)", sfud_dev->name, sfud_dev->chip.capacity);
        LOG_I("%s-->chip write_mode:%d", sfud_dev->name, sfud_dev->chip.write_mode);
        LOG_I("%s-->chip erase_gran:%d(byte)", sfud_dev->name, sfud_dev->chip.erase_gran);
        LOG_I("%s-->chip erase_gran_cmd:0x%x", sfud_dev->name, sfud_dev->chip.erase_gran_cmd);
    }

    int count = 1;
    /* set LED0 pin mode to output */
    // rt_pin_mode(LED0_PIN, PIN_MODE_OUTPUT);

    while (count++)
    {
        // rt_kprintf("aaa,count=%d, APP_VERSION: %s\n", count, APP_VERSION);
        // rt_pin_write(LED0_PIN, PIN_HIGH);
        rt_thread_mdelay(1000);
        // rt_pin_write(LED0_PIN, PIN_LOW);
    }

    return RT_EOK;
}
#define RT_APP_PART_ADDR 0x08020000
static int ota_app_vtor_reconfig(void)
{
    #define NVIC_VTOR_MASK   0x3FFFFF80
    /* Set the Vector Table base location by user application firmware definition */
    SCB->VTOR = RT_APP_PART_ADDR & NVIC_VTOR_MASK;

    return 0;
}
// INIT_BOARD_EXPORT(ota_app_vtor_reconfig);
